package com.whfc.mach.manager.impl;

import com.whfc.mach.constant.MachConstant;
import com.whfc.mach.dao.AppMachConfigMapper;
import com.whfc.mach.entity.AppMachConfig;
import com.whfc.mach.enums.MachTypeFence;
import com.whfc.mach.manager.MachConfigManager;
import com.whfc.mach.redis.MachPolygonWKTRedisDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author sun_guodong
 * @description
 * @date 2021-01-22
 */
@Service
public class MachConfigManagerImpl implements MachConfigManager {
    @Autowired
    private AppMachConfigMapper appMachConfigMapper;

    @Autowired
    private MachPolygonWKTRedisDao machPolygonWKTRedisDao;

    private static final String SHIP = "船";

    @Override
    public Integer getTimeOut(Integer deptId) {
        AppMachConfig appMachConfig = appMachConfigMapper.selectByDeptIdAndCode(deptId, MachConstant.TIME_OUT);
        Integer minutes = MachConstant.DEFAULT_TIME_OUT;
        if (appMachConfig != null) {
            minutes = Integer.parseInt(appMachConfig.getValue());
        }
        return minutes;
    }

    @Override
    public String getPolygonWKT(Integer deptId, MachTypeFence machTypeFence) {
        String polygonWKT = machPolygonWKTRedisDao.getPolygonWKT(deptId, machTypeFence.getCode());
        if (StringUtils.isEmpty(polygonWKT)) {
            AppMachConfig appMachConfig = appMachConfigMapper.selectByDeptIdAndCode(deptId, machTypeFence.getCode());
            if (appMachConfig == null) {
                // 如果没有船舶和车的就查所有的
                appMachConfig = appMachConfigMapper.selectByDeptIdAndCode(deptId, MachTypeFence.ALL.getCode());
            }
            if (appMachConfig != null) {
                polygonWKT = appMachConfig.getValue();
                machPolygonWKTRedisDao.setPolygonWKT(deptId, machTypeFence.getCode(), polygonWKT);
            }
        }
        return polygonWKT;
    }

    @Override
    public String getPolygonWKT(Integer deptId, String machTypeName) {
        if (StringUtils.isBlank(machTypeName)) {
            return this.getPolygonWKT(deptId, MachTypeFence.ALL);
        }
        if (machTypeName.contains(SHIP)) {
            return this.getPolygonWKT(deptId, MachTypeFence.SHIP);
        } else {
            return this.getPolygonWKT(deptId, MachTypeFence.CAR);
        }
    }

    @Override
    public String getLogo(Integer deptId) {
        AppMachConfig appMachConfig = appMachConfigMapper.selectByDeptIdAndCode(deptId, MachConstant.LOGO);
        if (appMachConfig == null || StringUtils.isBlank(appMachConfig.getValue())) {
            return "";
        }
        return appMachConfig.getValue();
    }
}
